package com.example.firstdemo.ui.screens.pages.problem.problemDetail

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.firstdemo.network.api.problem.ProblemApi
import com.example.firstdemo.network.api.problem.data.ConfirmData
import com.example.firstdemo.ui.screens.pages.problem.problemDetail.data.ProblemDetailUIState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class ProblemDetailViewModel : ViewModel() {
    private val _uiState = MutableStateFlow(ProblemDetailUIState())
    val uiState: StateFlow<ProblemDetailUIState> = _uiState

    fun getProblemDetail(passTime: String, uid: String, stationName: String) {
        viewModelScope.launch {
            try {
                val problem = ProblemApi.getDetail(passTime, uid).data!!
                _uiState.value = _uiState.value.copy(
                    problem = problem,
                    stationName = stationName
                )
            } catch (_: Exception) {
            }
        }
    }

    fun setDialogShow(show: Boolean) {
        _uiState.value = _uiState.value.copy(
            dialogShow = show
        )
    }

    fun confirm(data: ConfirmData, callback: () -> Unit = {}) {
        viewModelScope.launch {
            try {
                ProblemApi.confirm(data)
                callback()
            } catch (_: Exception) {
            }
        }
    }

//    fun confirm(data: ConfirmData) {
//        CoroutineScope(Dispatchers.IO).launch {
//            try {
//                ProblemApi.confirm(data)
//            } catch (_: Exception) {
//            }
//        }
//    }
}